
clear all
set maxvar 10000

** DEFINE PATH HERE
global main_path " "



cd "$main_path"
use "$main_path\Phaseout_all_surveyrounds.dta", clear

set more off

*note: if not installed, need to install packages cem and bmte


*** Initial impacts of the program ***

*** Table VI: No Treatment - Continuation balance ***
eststo clear
eststo notreat: quietly estpost summarize  age_member1_s5 education_level1_s5 cultivated_land_s5 cultivate_ownland_s5 title_s5 clothes_s5 shoes_s5 livestock_large_amt_s5 livestock_small_amt_s5 microfin_admin distances_mod if treatment==0
eststo cont: quietly estpost summarize  age_member1_s5 education_level1_s5 cultivated_land_s5 cultivate_ownland_s5 title_s5 clothes_s5 shoes_s5 livestock_large_amt_s5 livestock_small_amt_s5 microfin_admin distances_mod if treatment==1
eststo diff: quietly estpost ttest   age_member1_s5 education_level1_s5 cultivated_land_s5 cultivate_ownland_s5 title_s5 clothes_s5 shoes_s5 livestock_large_amt_s5 livestock_small_amt_s5 microfin_admin distances_mod if treatment<2, by(treatment) unequal
esttab  notreat cont diff using progimpact_tableVI.csv, cells("mean(pattern(1 1 0) fmt(3)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(3)) t(pattern(0 0 1) par fmt(3))") label replace

* The initial program impacts are estimated via OLS, IPW, MB-IPW and CEM. Code is provided for these one by one *


 ** OLS **

* OLS results in Table VII *
eststo clear
foreach var in impseed_s5 impseed_purchase_s5 localseed_s5  { 
eststo: areg `var' treatment distances_mod microfin_admin if treatment<2, absorb(bcode) cluster(ccode)
}
esttab _all using progimpact_OLS1.csv, cells(b(fmt(3) star) se(fmt(3)par) ci(fmt(3)par)) drop(_cons) r2(2) star(* 0.10 ** 0.05 *** 0.01) noobs label  plain  replace

* OLS results in Table VIII * 
eststo clear
foreach var in croprotation_s5 intercrop_s5 linesow_s5 orgfert_s5 chemfert_s5 weed_s5 pest_s5  { 
eststo: areg `var' treatment distances_mod microfin_admin if treatment<2, absorb(bcode) cluster(ccode)
}
esttab _all using progimpact_OLS2.csv, cells(b(fmt(3) star) se(fmt(3)par) ci(fmt(3)par)) drop(_cons) r2(2) star(* 0.10 ** 0.05 *** 0.01) noobs label  plain  replace


* OLS results for Table A.IX
foreach seas in 1 3 5 7 8 { 
areg impseed_s`seas' treatment microfin_admin distances_mod if treatment<2, absorb (bcode) cluster(ccode)
}

* OLS results for Table A.X
areg impseed_s5 treated brac_microfinance_member distances, absorb (bcode) cluster(ccode)
areg impseed_s5 treated brac_microfinance_member distances if treatment<2, absorb (bcode) cluster(ccode)
areg impseed_purchase_s5 treated brac_microfinance_member distances, absorb (bcode) cluster(ccode)
areg impseed_purchase_s5 treated brac_microfinance_member distances if treatment<2, absorb (bcode) cluster(ccode)


* OLS results in Tables AXI, AXII *
eststo clear
foreach var in  cropno_s5 grow_maize_s5  hiredlabor_s5 pesticide_s5 handplow_s5 mechplow_s5 use_pestpump_s5 { 
eststo: areg `var' treatment distances_mod microfin_admin if treatment<2, absorb(bcode) cluster(ccode)
}
esttab _all using progimpact_OLS3.csv, cells(b(fmt(3) star) ci(fmt(3)par)) drop(_cons) r2(2) star(* 0.10 ** 0.05 *** 0.01) noobs label  plain  replace


* Following code procudes the parallel results for the other estimation methods *


/*

 ** IPW and MB-IPW **
clear all
set maxvar 10000

use "$main_path\Phaseout_all_surveyrounds.dta", clear

tabulate branchdummy, gen(bcode_id)



** running both the comparison No Treatment - Continuation and No Treatment - All treated groups
gen cont_notreat=treatment
recode cont_notreat 2=. 3=.

foreach var in impseed_s5 impseed_purchase_s5 localseed_s5{ 
foreach type_treat in treated cont_notreat {
bmte `var' age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15 microfin_admin, group(`type_treat') psvars(age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15) bs reps(500)
}
}

foreach var in croprotation_s5 intercrop_s5 linesow_s5 orgfert_s5 chemfert_s5  weed_s5  pest_s5 {
bmte `var' age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15 microfin_admin, group(cont_notreat) psvars(age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15) bs reps(500)
}

foreach var in   cropno_s5 grow_maize_s5 hiredlabor_s5 pesticide_s5 handplow_s5 mechplow_s5   use_pestpump_s5 {
bmte `var' age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15 microfin_admin, group(cont_notreat) psvars(age_member1_s5 education_level1_s5 distances_mod bcode_id1-bcode_id15)  bs reps(500)
}


 ** CEM **
clear all
set maxvar 10000

use "$main_path\Phaseout_all_surveyrounds.dta", clear


tabulate branchdummy, gen(bcode_id)



preserve

* only comparing Continuation and No treatment, so dropping other groups
keep if treatment<2

* running CEM with pre-determined bins for education and distance. Education variable is in years, and the bins correspond to no education, 1-3 years of primary, 4-5 years of primary, finished primary (7 years), 3 years of secondary, all above. For distance, less than 500 metres, up to 2km, 5km, up to 10km, and above). Letting CEM find bins for age. 
cem age_member1_s5 education_level1_s5 (0.5 3.5 5.5 7.5 10.5 12.5 ) distances_mod (0.5 2 5 10) , treatment(treated)


* outcomes in main tables *
foreach var in impseed_s5 impseed_purchase_s5 localseed_s5  {
reg  `var' treated bcode_id1 bcode_id2 bcode_id3 bcode_id4 bcode_id5 bcode_id6 bcode_id7 bcode_id8 bcode_id9 bcode_id10 bcode_id11 bcode_id12 bcode_id13 bcode_id14  [iweight=cem_weights], cluster(ccode)
}

foreach var in croprotation_s5 intercrop_s5 linesow_s5 orgfert_s5 chemfert_s5 weed_s5 pest_s5 {
reg  `var' treated bcode_id1 bcode_id2 bcode_id3 bcode_id4 bcode_id5 bcode_id6 bcode_id7 bcode_id8 bcode_id9 bcode_id10 bcode_id11 bcode_id12 bcode_id13 bcode_id14  [iweight=cem_weights], cluster(ccode)
}


* outcomes in additional tables *
foreach var in cropno_s5 grow_maize_s5 hiredlabor_s5 pesticide_s5 handplow_s5 mechplow_s5   use_pestpump_s5 {
reg  `var' treated bcode_id1 bcode_id2 bcode_id3 bcode_id4 bcode_id5 bcode_id6 bcode_id7 bcode_id8 bcode_id9 bcode_id10 bcode_id11 bcode_id12 bcode_id13 bcode_id14  [iweight=cem_weights], cluster(ccode)
}

restore


